﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Microsoft.Reputation.Tests.FrontEndCommon
{
    public static class HexHelper
    {
        private static IEnumerable<int> BySteps(this int limit, int stepSize)
        {
            for (int i = 0; i < limit; i += stepSize)
            {
                yield return i;
            }
        }

        public static byte[] HexToBytes(this string hexString)
        {
            if (String.IsNullOrEmpty(hexString))
            {
                return null;
            }
            var byteChunks = hexString.Length.BySteps(2);
            if (hexString.StartsWith("0x"))
            {
                byteChunks = byteChunks.Skip(1);
            }
            return byteChunks.Select(p => Byte.Parse(hexString.Substring(p, 2), System.Globalization.NumberStyles.HexNumber)).ToArray();
        }
    }
}
